//
//  MSCallAlterView.m
//  WeShare2.0
//
//  Created by Mac_User on 15/11/12.
//  Copyright © 2015年 WeShare. All rights reserved.
//

#import "MSCallAlterView.h"
@interface MSCallAlterView ()
@property (nonatomic, strong)UIView *backgroundView;
@property (nonatomic, strong) UIButton *callBtn;

@end
@implementation MSCallAlterView
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame= CGRectMake(0, 0, SCREEN_WIDTH - 60, 140);
        self.centerY = SCREEN_HEIGHT / 2;
        self.centerX = SCREEN_WIDTH / 2;
        [self initUI];
    }
    return self;
}

- (void)initUI
{
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 60, 140)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:whiteView];

    self.callBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.callBtn.frame = CGRectMake(20, 20, whiteView.width - 40, 40);
    [self.callBtn setBackgroundColor:kYellowColor];
    [self.callBtn.titleLabel setFont:[UIFont fontWithName:@"STHeitiSC-Light" size:15]];
    self.callBtn.layer.masksToBounds = YES;
    self.callBtn.layer.cornerRadius = 3;
    [self.callBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.callBtn setTitleColor:kLightGrayColor forState:UIControlStateHighlighted];
    [self.callBtn addTarget:self action:@selector(callBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:self.callBtn];
    
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame = CGRectMake(20, self.callBtn.bottom + 20, whiteView.width - 40, 40);
    [cancleBtn setBackgroundColor:[UIColor colorWithRed:232 / 255. green:232 / 255. blue:232 / 255. alpha:1]];
    cancleBtn.layer.masksToBounds = YES;
    cancleBtn.layer.cornerRadius = 3;
    [cancleBtn setTitleColor:kDarkGrayColor forState:UIControlStateNormal];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    [cancleBtn setTitleColor:kLightGrayColor forState:UIControlStateHighlighted];
    [cancleBtn addTarget:self action:@selector(cancleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:cancleBtn];
    
    self.height = cancleBtn.bottom + 20;

}

- (void)setPhoneNumber:(NSString *)phoneNumber
{
    _phoneNumber = phoneNumber;
    [self.callBtn setTitle:[NSString stringWithFormat:@"拨打%@",phoneNumber] forState:UIControlStateNormal];
}
- (void)show
{
    if (!self.backgroundView) {
        self.backgroundView = [[UIView alloc] initWithFrame:SCREENFRAME];
        self.backgroundView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.6f];
        [self.backgroundView addSubview:self];
    }
    
    if (self.backgroundView.superview) {
        [self.backgroundView removeFromSuperview];
    }
    
    if (!self.backgroundView.superview) {
        UIWindow *window = [UIApplication sharedApplication].windows[0];
        [window addSubview:self.backgroundView];
    }
    
    
    
}
- (void)callBtnClick:(UIButton *)btn
{
    [self dismiss];
    [HDUtil call:self.phoneNumber];
}

- (void)cancleBtnClick:(UIButton *)btn
{
    [UIView animateWithDuration:4 animations:^{
        
        [self dismiss];
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.3 animations:^{
    [self.backgroundView removeFromSuperview];
    }];
   
}
@end
